package com.example.jasonsun.sharelib.platform;

import android.app.Activity;
import android.os.Bundle;

import com.tencent.connect.share.QzoneShare;
import com.tencent.open.utils.ThreadManager;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.util.ArrayList;

/**
 * QQ空间
 */
public class QZonePlatform implements SharePlatform {
    private Activity mActivity;
    public static Tencent mTencent;
    public static String mAppid = "222222";
    //QZone分享， SHARE_TO_QQ_TYPE_DEFAULT 图文，SHARE_TO_QQ_TYPE_IMAGE 纯图
    private int shareType = QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT;

    public QZonePlatform(Activity activity) {
        mActivity = activity;
        if (mTencent == null) {
            mTencent = Tencent.createInstance(mAppid, mActivity);
        }
    }

    @Override
    public void share(int platform, String title, String content, String imgUrl, String webUrl) {
        if (platform != SharePlatform.QZONE) {
            throw new IllegalArgumentException("platform wrong!");
        }

        final Bundle params = new Bundle();
        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, shareType);
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, content);
        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, webUrl);

        ArrayList<String> imageUrls = new ArrayList<String>();
        imageUrls.add(imgUrl);
        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);

        if (shareType == QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT) {
            doShareToQzone(params);
        } else {
            doPublishToQzone(params);
        }
    }

    /**
     * 用异步方式启动分享
     *
     * @param params
     */
    private void doShareToQzone(final Bundle params) {
        // QZone分享要在主线程做
        ThreadManager.getMainHandler().post(new Runnable() {
            @Override
            public void run() {
                if (null != mActivity) {
                    mTencent.shareToQzone(mActivity, params, qZoneShareListener);
                }
            }
        });
    }

    private void doPublishToQzone(final Bundle params) {
        ThreadManager.getMainHandler().post(new Runnable() {
            @Override
            public void run() {
                if (null != mActivity) {
                    mTencent.publishToQzone(mActivity, params, qZoneShareListener);
                }
            }
        });
    }

    public IUiListener qZoneShareListener = new IUiListener() {
        @Override
        public void onCancel() {
            QQUtil.toastMessage(mActivity, "onCancel:test ");
        }

        @Override
        public void onError(UiError e) {
            // TODO Auto-generated method stub
            QQUtil.toastMessage(mActivity, "onError: " + e.errorMessage, "e");
        }

        @Override
        public void onComplete(Object response) {
            // TODO Auto-generated method stub
            QQUtil.toastMessage(mActivity, "onComplete: " + response.toString());
        }

    };


    @Override
    public void login() {
        throw new UnsupportedOperationException("此平台不能用于登录， 请用QQ平台");
    }

    @Override
    public void logout() {
        throw new UnsupportedOperationException("此平台不能注销， 请用QQ平台");
    }
}
